// import { defineConfig } from 'vite'
// import vue from '@vitejs/plugin-vue'
//
// // https://vitejs.dev/config/
// export default defineConfig({
//   plugins: [vue()]
// })

import { defineConfig, loadEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// import styleImport from 'vite-plugin-style-import'
// import fs from "fs"
// import dotenv from 'dotenv'
import * as Path from 'path'

export default defineConfig(({ mode }: UserConfig): UserConfig => {

  // const GVA_ENV = dotenv.parse(fs.readFileSync(`.env.${mode}`))

  return {
    resolve: {
      alias: {
        '@assets': Path.resolve(__dirname, './src/assets'),
        '@common': Path.resolve(__dirname, './src/common'),
        '@components': Path.resolve(__dirname, './src/components'),
        '@config': Path.resolve(__dirname, './src/config'),
        '@i18n': Path.resolve(__dirname, './src/i18n'),
        '@pages': Path.resolve(__dirname, './src/pages'),
        '@router': Path.resolve(__dirname, './src/router'),
        '@store': Path.resolve(__dirname, './src/store'),
        '@types': Path.resolve(__dirname, './src/types'),
        '@utils': Path.resolve(__dirname, './src/utils'),
        "@": Path.resolve(__dirname, "src")
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/global.scss";',
        },
      },
    },
    server: {
      // port: Number(GVA_ENV.VITE_BASE_CLI_PORT),
      port: 8989,
      // 是否自动在浏览器打开
      open: true,
      // 是否开启 https
      https: false,
      proxy: {
        '/api': {
          //target: loadEnv(mode, process.cwd().VITE_APP_BASE_URL) || 'http://localhost:9000/',
          target: 'http://localhost:9000/',
          changeOrigin: true,
          rewrite: (pathStr) => pathStr.replace(/^\/api/, '')
        }
      },
      hmr: {
        overlay: false,
      }
    },
    optimizeDeps: {
      include: [
        "element-plus",
        "element-plus/lib/locale/lang/zh-cn",
        "dayjs/locale/zh-cn",
      ],
    },
    build: {
      // 压缩
      minify: "esbuild",
      assetsDir: "",
      outDir: `./dist/${process.env.VITE_ENV}`,
    },
    plugins: [
      vue(),
      // styleImport({
      //   libs: [{
      //     libraryName: 'element-plus',
      //     esModule: true,
      //     ensureStyleFile: true,
      //     resolveStyle: (name) => {
      //       name = name.slice(3)
      //       return `element-plus/packages/theme-chalk/src/${name}.scss`;
      //     },
      //     resolveComponent: (name) => {
      //       return `element-plus/lib/${name}`;
      //     },
      //   }]
      // })
    ]
  }
})
